package com.dy.controller;

import com.dy.domain.EchartsTu;
import com.dy.domain.TuShu;
import com.dy.domain.pojo.PageBean;
import com.dy.domain.pojo.Result;
import com.dy.service.TuShuService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/tushu")
public class TuShuController {

    @Autowired
    private TuShuService tushuService;

    @RequestMapping("/findPage")
    public Result findPage(Integer pageNo,Integer pageSize,String ming){

        PageBean<TuShu> tushu = tushuService.findPage(pageNo,pageSize,ming);

        return new Result().ok(tushu);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody TuShu tuShu){
        tushuService.add(tuShu);
        return new Result().ok(null);
    }

    @RequestMapping("/huixian")
    public Result huixian(Integer tid){
        TuShu tuShu = tushuService.huixian(tid);
        return new Result().ok(tuShu);
    }

    @RequestMapping("/update")
    public Result update(@RequestBody TuShu tuShu){
        tushuService.update(tuShu);
        return new Result().ok(null);
    }

    @RequestMapping("/del")
    public Result del(Integer tid){
        tushuService.del(tid);
        return new Result().ok(null);
    }

    @RequestMapping("/cha")
    public EchartsTu cha(){
        return tushuService.cha();
    }

    @RequestMapping("/daochu")
    public void daochu(HttpServletResponse response) throws IOException {

        HSSFWorkbook workbook = new HSSFWorkbook();

        HSSFSheet shu_list = workbook.createSheet("shu_list");

        HSSFRow row = shu_list.createRow(0);

        row.createCell(0).setCellValue("编号");
        row.createCell(1).setCellValue("图书名称");
        row.createCell(2).setCellValue("价格");
        row.createCell(3).setCellValue("作者");
        row.createCell(4).setCellValue("分类");

        List<TuShu> list = tushuService.findall();

        int i = 1;
        for (TuShu tuShu : list) {

            HSSFRow row1 = shu_list.createRow(i++);
            row1.createCell(0).setCellValue(tuShu.getTid());
            row1.createCell(1).setCellValue(tuShu.getBookname());
            row1.createCell(2).setCellValue(tuShu.getPrice());
            row1.createCell(3).setCellValue(tuShu.getAuthor());
            row1.createCell(4).setCellValue(tuShu.getLei());
        }

        //处理文件下载
        response.setContentType("application/octet-stream; charset=utf-8");
        response.setHeader("Content-Disposition", "attachment; filename=shu_zhanshi.xlsx");
        //写数据
        workbook.write(response.getOutputStream());
    }

    //导入
    @RequestMapping("/daoru")
    public void daoru(MultipartFile file,HttpServletResponse response) throws IOException {

        HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());

        HSSFSheet sheetAt = workbook.getSheetAt(0);

        int i = 0;

        for (Row cells : sheetAt) {

            if (i==0){
                i++;
                continue;
            }

            String bookname = cells.getCell(1).getStringCellValue();
            String price = cells.getCell(2).getStringCellValue();
            String author = cells.getCell(3).getStringCellValue();
            String lei = cells.getCell(4).getStringCellValue();

            tushuService.add(new TuShu(null,bookname,price,author,lei));
        }

        response.sendRedirect("/admin/tushu_list.html");

    }

}
